package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.projectcore.dto.DictPageQuery;
import com.eastfair.projectcore.dto.DictSaveDTO;
import com.eastfair.projectcore.dto.DictUpdateDTO;
import com.eastfair.projectcore.entity.Dict;
import com.eastfair.projectcore.service.DictService;
import com.eastfair.api.NoticeServiceFeign;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 字典表
 * </p>
 *
 * @author ligang
 * @date 2021-07-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dict")
@Api(value = "Dict", tags = "字典表")
public class DictController extends SuperController<DictService, Long, Dict, DictPageQuery, DictSaveDTO, DictUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private DictService dictService;
    @Autowired
    private NoticeServiceFeign noticeServiceFeign;

    @Override
    public void handlerResult(IPage<Dict> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Dict> dictList = list.stream().map((map) -> {
            Dict dict = Dict.builder().build();
            //TODO 请在这里完成转换
            return dict;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(dictList));
    }

    /**
     * 字典查询
     *
     * @return
     */
    @PostMapping("findByNames")
    public Map<Serializable, Object> findByNames(@RequestBody Set<Serializable> types) {
        System.out.println("111");
        return baseService.findNameByIds(types);
    }

    /**
     * 字典ID查询
     *
     * @return
     */
    @GetMapping("findByIds")
    public Map<Serializable, Object> findByIds(Set<Serializable> set) {
        return baseService.findByIds(set);
    }

}